package com.example.demo.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.entity.AfterSaleTargetZ1;
import com.example.demo.entity.AfterSaleTargetZ2;
import com.example.demo.entity.SysDept;
import com.example.demo.entity.SysUser;
import com.example.demo.form.AfterSaleTargetZ2Form;
import com.example.demo.service.AfterSaleTargetZ1Service;
import com.example.demo.service.AfterSaleTargetZ2Service;
import com.example.demo.service.SysDeptService;
import com.example.demo.service.SysUserService;

@RestController
@RequestMapping(value = "/AfterSaleTargetZ2Controller")
public class AfterSaleTargetZ2Controller {
	
	@Autowired
	private AfterSaleTargetZ2Service service2;
	@Autowired
	private AfterSaleTargetZ1Service service1;
	@Autowired
	private SysUserService sysUserDAO;
	@Autowired
	private SysDeptService deptDAO;
	private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
	
	@RequestMapping(value = "/save")
	private Object save(AfterSaleTargetZ2Form Form) throws java.text.ParseException {
		Integer saleId1 = Form.getId();
		Date d= new Date();
		Date d1 = dateFormat1.parse("1999-01-01");
		AfterSaleTargetZ1 sale1= new AfterSaleTargetZ1();
		AfterSaleTargetZ1 Sale1= new AfterSaleTargetZ1();
		AfterSaleTargetZ2 sale2= new AfterSaleTargetZ2();
		AfterSaleTargetZ2 Sale2= new AfterSaleTargetZ2();
		SysUser myUser = sysUserDAO.findById(Form.getUserId());
		AfterSaleTargetZ1 t1=service1.findByDateAndUser(Form.getDate(),myUser);
		System.out.println(Form.getDate());
		if(t1==null) {
			Sale1.setDate(Form.getDate());
			SysUser user=sysUserDAO.findById(Form.getUserId());
			Sale1.setUser(user);
			t1 = service1.save(Sale1);
		}else {
			 sale1 = service1.findById(t1.getId());
			 sale1.setDate(Form.getDate());
				SysUser user=sysUserDAO.findById(Form.getUserId());
				Sale1.setUser(user);
				service1.save(sale1);
				List<AfterSaleTargetZ2> list = service2.findByUser1(sale1);
				for (AfterSaleTargetZ2 AfterSaleTargetZ2 : list) {
					if(d1.compareTo(AfterSaleTargetZ2.getBeginDate())==-1) {
						d1=AfterSaleTargetZ2.getBeginDate();
						Sale2=AfterSaleTargetZ2;
					}
				}
				Sale2.setEndDate(d);
				service2.save(Sale2);
		}
		BeanUtils.copyProperties(Form, sale2, "id");
		sale2.setUser1(t1);
		sale2.setBeginDate(d);
		service2.save(sale2);
		return new AjaxResult("数据保存成功");			
	}
	
	@RequestMapping(value = "/findByuserId")
	public Object findByuserId(AfterSaleTargetZ2Form form) throws ParseException {
		Integer userId = form.getUserId();
		Date date = form.getDate();
		Integer erBiaoId ;
		AfterSaleTargetZ1 user1 = service1.findByUserIdAndDate(userId,date);
		if(user1!=null) {
			erBiaoId = user1.getId();
			AfterSaleTargetZ2 Sale2= new AfterSaleTargetZ2();
			Date d1 = dateFormat1.parse("1999-01-01");
			List<AfterSaleTargetZ2> list = service2.findByUser1Id(erBiaoId);
			for (AfterSaleTargetZ2 AfterSaleTargetZ2 : list) {
				if(d1.compareTo(AfterSaleTargetZ2.getBeginDate())==-1) {
					d1=AfterSaleTargetZ2.getBeginDate();
					Sale2=AfterSaleTargetZ2;
				}
			}
			return new AjaxResult(Sale2);
		}else {
			return new AjaxResult(false,"该人员还未录入");
		}

	}
	
	@RequestMapping(value = "/findShouHou")
	public Object findShouHou() {
		List<SysDept> allDept = deptDAO.findAll();
		for (SysDept sysDept : allDept) {
			if(sysDept.getParentId()==null&&"售后部".equals(sysDept.getName())) {
				Integer nameId = sysDept.getId();
				List<SysUser> a = sysUserDAO.findByDeptId(nameId);
//				for (SysUser a2 : a) {s
//					if("销售顾问".equals(a2.getZhiwei())) {
//						
//					}
//				}
				return new AjaxResult(a);
			}
		}
		return new AjaxResult(false);
	}
	
	@RequestMapping(value = "/findxiaoShou")
	public Object findxiaoShou() {
		List<SysDept> allDept = deptDAO.findAll();
		for (SysDept sysDept : allDept) {
			if(sysDept.getParentId()==null&&"销售部".equals(sysDept.getName())) {
				Integer nameId = sysDept.getId();
				List<SysUser> a = sysUserDAO.findByDeptId(nameId);
				return new AjaxResult(a);
			}
		}
		return new AjaxResult(false);
	}
	
}
